//
//  Image.swift
//  
//
//  Created by bo wang on 2020/10/21.
//  Copyright © 2020 wangbo. All rights reserved.
//

import Foundation
import UIKit

extension UIImage {
    static func imageWith(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage? {
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContext(size)
        let ctx = UIGraphicsGetCurrentContext()
        ctx?.setFillColor(color.cgColor)
        ctx?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    static func getScreenShot(from: UIView) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(from.bounds.size, false, 0)
        from.drawHierarchy(in: from.bounds, afterScreenUpdates: false)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}
